<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class aktifitas_model extends CI_Model {

    function __construct() 
	{
        parent::__construct();
        $this->CI = get_instance();
    }

    function insert($data) 
	{
        $this->db->insert('aktifitas', $data);
    }
	
	function update($id, $data) 
	{
        $this->db->where('id_aktifitas', $id);
        $this->db->update('aktifitas', $data);
    }
	
    function delete($id) 
	{
        $this->db->where('id_aktifitas', $id);
        $this->db->delete('aktifitas');
    }
	
	function get_data_all_aktifitas()
	{
		$this->db->select("*");
		$this->db->from('aktifitas');
		$query = $this->db->get();
		return $query;
	}

    //list flexy
    function get_data_aktifitas() 
	{
        //Select table name
        $table_name = "aktifitas";

        //Build contents query
        $this->db->select('*')->from($table_name);
		
        $this->CI->flexigrid->build_query();

        //Get contents
        $return['records'] = $this->db->get();

        //Build count query
        $this->db->select('*')->from($table_name);
		$this->CI->flexigrid->build_query(FALSE);

        $return['record_count'] = $this->db->count_all_results();

        //Return all
        return $return;
    }

    function selectone($id) 
	{
        $this->db->select("*");
        $this->db->from('aktifitas');
        $this->db->where('ID_AKTIFITAS', $id);
        $query = $this->db->get();
        return $query;
    }
		
	function selectone2() 
	{
		$this->db->select("*");
        $this->db->from('aktifitas');
		$this->db->join('satuan_jenis_aktifitas AS SJA','aktifitas.id_satuan_jenis_aktifitas = SJA.id_satuan_jenis_aktifitas','left');
		// $this->db->where('id_satuan_jenis_aktifitas', $id_satuan_jenis_aktifitas);        
        $query = $this->db->get();
        return $query;
	}
	
	function get_data_proses_aktifitas($id_satuan_jenis_aktifitas, $ID_EVALUASI) 
	{
        //Select table name
        //$table_name = "aktifitas";

        //Build contents query
		$this->db->select('s.*, ks.*' );
		$this->db->from('aktifitas ks');
		$this->db->join('proses_aktifitas as s', 'ks.id_proses_aktifitas = s.id_proses_aktifitas', 'left');
		$this->db->join('evaluasi as e', 'ks.ID_EVALUASI = e.ID_EVALUASI', 'left');
		$this->db->where('ks.id_satuan_jenis_aktifitas', $id_satuan_jenis_aktifitas);
		$this->db->where('ks.ID_EVALUASI', $ID_EVALUASI);
		$this->CI->flexigrid->build_query();		
		
        //Get contents
        $return['records'] = $this->db->get();

        //Build count query
        $this->db->select('s.*, ks.*');
		$this->db->from('aktifitas ks');
		$this->db->join('proses_aktifitas as s', 'ks.id_proses_aktifitas = s.id_proses_aktifitas', 'left');
		$this->db->join('evaluasi as e', 'ks.ID_EVALUASI = e.ID_EVALUASI', 'left');
		$this->db->where('ks.id_satuan_jenis_aktifitas', $id_satuan_jenis_aktifitas);
		$this->db->where('ks.ID_EVALUASI', $ID_EVALUASI);
		$this->CI->flexigrid->build_query(FALSE);

        $return['record_count'] = $this->db->count_all_results();

        //Return all
        return $return;
    }
	
	function get_nama_proses_aktifitas($id_satuan_jenis_aktifitas) 
	{
        //Build contents query
		$this->db->select('s.*, ks.*');
		$this->db->from('proses_aktifitas ks');
		$this->db->join('satuan_jenis_aktifitas as s', 'ks.id_satuan_jenis_aktifitas = s.id_satuan_jenis_aktifitas', 'left');
		// $this->db->join('aktifitas as sf', 'ks.id_proses_aktifitas = sf.id_proses_aktifitas', 'left');
		$this->db->where('ks.id_satuan_jenis_aktifitas', $id_satuan_jenis_aktifitas);
		$this->CI->flexigrid->build_query();		
		
        //Get contents
        $return['records'] = $this->db->get();

        //Build count query
        $this->db->select('s.*, ks.*');
		$this->db->from('proses_aktifitas ks');
		$this->db->join('satuan_jenis_aktifitas as s', 'ks.id_satuan_jenis_aktifitas = s.id_satuan_jenis_aktifitas', 'left');
		// $this->db->join('aktifitas as sf', 'ks.id_proses_aktifitas = sf.id_proses_aktifitas', 'left');
		$this->db->where('ks.id_satuan_jenis_aktifitas', $id_satuan_jenis_aktifitas);
		$this->CI->flexigrid->build_query(FALSE);

        $return['record_count'] = $this->db->count_all_results();

        //Return all
        return $return;
    }
	
	function get_total_pv($id_satuan_jenis_aktifitas) 
	{
        //Build contents query
		$this->db->select('s.*, ks.*, a.SUM(persentase_volume)/COUNT(id_proses_aktifitas) AS TP1');
		$this->db->from('aktifitas ks');
		$this->db->join('satuan_jenis_aktifitas as s', 'ks.id_satuan_jenis_aktifitas = s.id_satuan_jenis_aktifitas', 'left');
		// $this->db->join('aktifitas as sf', 'ks.id_proses_aktifitas = sf.id_proses_aktifitas', 'left');
		$this->db->where('ks.id_satuan_jenis_aktifitas', $id_satuan_jenis_aktifitas);
		$this->CI->flexigrid->build_query();		
		
        //Get contents
        $return['records'] = $this->db->get();

        //Build count query
        $this->db->select('s.*, ks.*, a.SUM(persentase_volume)/COUNT(id_proses_aktifitas) AS TP1');
		$this->db->from('aktifitas ks');
		$this->db->join('satuan_jenis_aktifitas as s', 'ks.id_satuan_jenis_aktifitas = s.id_satuan_jenis_aktifitas', 'left');
		// $this->db->join('aktifitas as sf', 'ks.id_proses_aktifitas = sf.id_proses_aktifitas', 'left');
		$this->db->where('ks.id_satuan_jenis_aktifitas', $id_satuan_jenis_aktifitas);
		$this->CI->flexigrid->build_query(FALSE);

        $return['record_count'] = $this->db->count_all_results();

        //Return all
        return $return;
    }
	
	function data_form_nilai($id_satuan_jenis_aktifitas, $ID_EVALUASI)
	{
		$this->db->select('s.*, ks.*');
		$this->db->from('aktifitas ks');
		$this->db->join('proses_aktifitas as s', 'ks.id_proses_aktifitas = s.id_proses_aktifitas', 'left');
		$this->db->where('ks.id_satuan_jenis_aktifitas', $id_satuan_jenis_aktifitas);
		$this->db->where('ks.ID_EVALUASI', $ID_EVALUASI);
		$this->db->order_by('s.nama_proses_aktifitas', 'asc');
		$query = $this->db->get();
        return $query;
	}
	
	function save_nilai($id_proses_aktifitas, $id_satuan_jenis_aktifitas, $data) 
	{
        $this->db->where('id_proses_aktifitas', $id_proses_aktifitas);
        $this->db->where('id_satuan_jenis_aktifitas', $id_satuan_jenis_aktifitas);
        $this->db->update('aktifitas', $data);
    }
	
	function get_daftar_mesinprod($perusahaan_id)
	{
		$query = $this->db->query("SELECT * FROM DISPERDAGINDB.IUI_MESIN where TIPE_MESIN = '1' AND ID_M_PERUSAHAAN = ".$perusahaan_id);				
		return $query;
	}
	
	function nilai_kv($id_proses_aktifitas)
	{			
		$cek = $this->db->query("SELECT * FROM aktifitas where id_proses_aktifitas = ".$id_proses_aktifitas." and ID_EVALUASI = ".$this->session->userdata('ID_EVALUASI')."");
		return $cek; 
	}	
	
	function nilai_kvpe($id_proses_aktifitas)
	{			
		$cek_data = $this->db->query("SELECT kuota_volume, volume_existing, (volume_existing/kuota_volume)*100 AS PS FROM aktifitas where id_proses_aktifitas = ".$id_proses_aktifitas." and ID_EVALUASI = ".$this->session->userdata('ID_EVALUASI')."");
		return $cek_data; 
	}	
	
	function nilai_total_progress($id_satuan_jenis_aktifitas)
	{			
		$cek_tp = $this->db->query("SELECT SUM(persentase_volume)/COUNT(id_proses_aktifitas) AS TP1 FROM aktifitas where id_satuan_jenis_aktifitas = ".$id_satuan_jenis_aktifitas." and ID_EVALUASI = ".$this->session->userdata('ID_EVALUASI')."");
		return $cek_tp; 
	}	
			
}	

?>